Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_ViewportHandler.h
blob50a2fde8dccae79af4e92c3eec760e37b32e7fd8
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_VIEWPORTHANDLER_JUCEHEADER__
27 #define __JUCER_VIEWPORTHANDLER_JUCEHEADER__
30 //==============================================================================
31 /**
33 class ViewportHandler : public ComponentTypeHandler
35 public:
36 //==============================================================================
37 ViewportHandler()
38 : ComponentTypeHandler ("Viewport", "Viewport", typeid (UpdatingViewport), 150, 150)
41 //==============================================================================
42 Component* createNewComponent (JucerDocument*)
44 Viewport* const v = new UpdatingViewport ("new viewport");
45 v->setViewedComponent (new ViewportDemoContentComp());
47 return v;
50 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
52 Viewport* const v = dynamic_cast <Viewport*> (comp);
53 XmlElement* const e = ComponentTypeHandler::createXmlFor (comp, layout);
55 e->setAttribute ("vscroll", v->isVerticalScrollBarShown());
56 e->setAttribute ("hscroll", v->isHorizontalScrollBarShown());
57 e->setAttribute ("scrollbarThickness", v->getScrollBarThickness());
59 e->setAttribute ("contentType", getViewportContentType (v));
60 e->setAttribute ("jucerFile", getViewportJucerComponentFile (v));
61 e->setAttribute ("contentClass", getViewportGenericComponentClass (v));
62 e->setAttribute ("constructorParams", getViewportConstructorParams (v));
64 return e;
67 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
69 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
70 return false;
72 Viewport defaultViewport;
73 Viewport* const v = dynamic_cast <Viewport*> (comp);
74 v->setScrollBarsShown (xml.getBoolAttribute ("vscroll", defaultViewport.isVerticalScrollBarShown()),
75 xml.getBoolAttribute ("hscroll", defaultViewport.isHorizontalScrollBarShown()));
77 v->setScrollBarThickness (xml.getIntAttribute ("scrollbarThickness", defaultViewport.getScrollBarThickness()));
79 setViewportJucerComponentFile (v, xml.getStringAttribute ("jucerFile", String::empty));
80 setViewportGenericComponentClass (v, xml.getStringAttribute ("contentClass"));
81 setViewportContentType (v, xml.getIntAttribute ("contentType", 0));
82 setViewportConstructorParams (v, xml.getStringAttribute ("constructorParams"));
84 return true;
87 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
89 ComponentTypeHandler::getEditableProperties (component, document, properties);
91 Viewport* const v = dynamic_cast <Viewport*> (component);
93 properties.add (new ViewportScrollbarShownProperty (v, document, true));
94 properties.add (new ViewportScrollbarShownProperty (v, document, false));
95 properties.add (new ViewportScrollbarSizeProperty (v, document));
96 properties.add (new ViewportContentTypeProperty (v, document));
98 if (getViewportContentType (v) == 1)
100 properties.add (new ViewportJucerFileProperty (v, document));
101 properties.add (new ConstructorParamsProperty (v, document));
103 else if (getViewportContentType (v) == 2)
105 properties.add (new ViewportContentClassProperty (v, document));
106 properties.add (new ConstructorParamsProperty (v, document));
110 const String getCreationParameters (Component* comp)
112 return quotedString (comp->getName());
115 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
117 Viewport defaultViewport;
118 Viewport* const v = dynamic_cast <Viewport*> (component);
120 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
122 if (defaultViewport.isVerticalScrollBarShown() != v->isVerticalScrollBarShown()
123 || defaultViewport.isHorizontalScrollBarShown() != v->isHorizontalScrollBarShown())
125 code.constructorCode
126 << memberVariableName << "->setScrollBarsShown ("
127 << boolToString (v->isVerticalScrollBarShown()) << ", "
128 << boolToString (v->isHorizontalScrollBarShown()) << ");\n";
131 if (defaultViewport.getScrollBarThickness() != v->getScrollBarThickness())
133 code.constructorCode
134 << memberVariableName << "->setScrollBarThickness ("
135 << v->getScrollBarThickness() << ");\n";
138 if (getViewportContentType (v) != 0)
140 String className (getViewportGenericComponentClass (v));
142 if (getViewportContentType (v) == 1)
144 File file;
146 const String filename (getViewportJucerComponentFile (v));
148 if (filename.isNotEmpty())
149 file = code.document->getFile().getSiblingFile (filename);
151 JucerDocument* doc = ObjectTypes::loadDocumentFromFile (file, false);
153 if (doc != 0)
155 code.includeFilesCPP.add (doc->getFile().withFileExtension ("h")
156 .getRelativePathFrom (code.document->getFile().getParentDirectory())
157 .replaceCharacter ('\\', '/'));
159 className = doc->getClassName();
160 delete doc;
162 else
164 className = String::empty;
168 if (className.isNotEmpty())
170 code.constructorCode
171 << memberVariableName << "->setViewedComponent (new "
172 << className;
174 if (getViewportConstructorParams (v).trim().isNotEmpty())
176 code.constructorCode << " (" << getViewportConstructorParams (v).trim() << "));\n";
178 else
180 code.constructorCode << "());\n";
185 code.constructorCode << "\n";
188 static void updateViewportContentComp (Viewport* vp)
190 if (getViewportContentType (vp) == 1)
192 JucerDocument* doc = findParentDocument (vp);
193 TestComponent* tc = new TestComponent (doc, 0, false);
195 tc->setFilename (getViewportJucerComponentFile (vp));
196 tc->setToInitialSize();
198 vp->setViewedComponent (tc);
200 else
202 vp->setViewedComponent (new ViewportDemoContentComp());
206 static int getViewportContentType (Viewport* vp)
208 return vp->getProperties() ["contentType"];
211 static void setViewportContentType (Viewport* vp, int newValue)
213 if (newValue != getViewportContentType (vp))
215 vp->getProperties().set ("contentType", newValue);
216 updateViewportContentComp (vp);
220 static const String getViewportJucerComponentFile (Viewport* vp)
222 return vp->getProperties() ["jucerFile"].toString();
225 static void setViewportJucerComponentFile (Viewport* vp, const String& file)
227 if (file != getViewportJucerComponentFile (vp))
229 vp->getProperties().set ("jucerFile", file);
230 updateViewportContentComp (vp);
234 static const String getViewportGenericComponentClass (Viewport* vp)
236 return vp->getProperties() ["contentClass"].toString();
239 static void setViewportGenericComponentClass (Viewport* vp, const String& className)
241 if (className != getViewportGenericComponentClass (vp))
243 vp->getProperties().set ("contentClass", className);
244 updateViewportContentComp (vp);
248 static const String getViewportConstructorParams (Viewport* vp)
250 return vp->getProperties() ["constructorParams"].toString();
253 static void setViewportConstructorParams (Viewport* vp, const String& newParams)
255 if (newParams != getViewportConstructorParams (vp))
257 vp->getProperties().set ("constructorParams", newParams);
258 updateViewportContentComp (vp);
262 private:
263 //==============================================================================
264 class UpdatingViewport : public Viewport
266 public:
267 UpdatingViewport (const String& name)
268 : Viewport (name)
272 void parentHierarchyChanged()
274 Viewport::parentHierarchyChanged();
275 updateViewportContentComp (this);
279 //==============================================================================
280 class ViewportDemoContentComp : public Component
282 public:
283 ViewportDemoContentComp()
285 setSize (2048, 2048);
288 void paint (Graphics& g)
290 g.fillCheckerBoard (getLocalBounds(), 50, 50,
291 Colours::lightgrey.withAlpha (0.5f),
292 Colours::darkgrey.withAlpha (0.5f));
296 //==============================================================================
297 class ViewportScrollbarShownProperty : public ComponentBooleanProperty <Viewport>
299 public:
300 ViewportScrollbarShownProperty (Viewport* comp, JucerDocument& document, const bool vertical_)
301 : ComponentBooleanProperty <Viewport> (vertical_ ? "V scrollbar" : "H scrollbar",
302 "enabled", "enabled",
303 comp, document),
304 vertical (vertical_)
308 void setState (bool newState)
310 document.perform (new ViewportScrollbarChangeAction (component, *document.getComponentLayout(), vertical, newState),
311 "Change Viewport scrollbar");
314 bool getState() const
316 return vertical ? component->isVerticalScrollBarShown()
317 : component->isHorizontalScrollBarShown();
320 const bool vertical;
322 private:
323 class ViewportScrollbarChangeAction : public ComponentUndoableAction <Viewport>
325 public:
326 ViewportScrollbarChangeAction (Viewport* const comp, ComponentLayout& layout, const bool vertical_, const bool newState_)
327 : ComponentUndoableAction <Viewport> (comp, layout),
328 vertical (vertical_),
329 newState (newState_)
331 oldState = vertical ? comp->isVerticalScrollBarShown()
332 : comp->isHorizontalScrollBarShown();
335 bool perform()
337 showCorrectTab();
338 if (vertical)
339 getComponent()->setScrollBarsShown (newState, getComponent()->isHorizontalScrollBarShown());
340 else
341 getComponent()->setScrollBarsShown (getComponent()->isVerticalScrollBarShown(), newState);
343 changed();
344 return true;
347 bool undo()
349 showCorrectTab();
350 if (vertical)
351 getComponent()->setScrollBarsShown (oldState, getComponent()->isHorizontalScrollBarShown());
352 else
353 getComponent()->setScrollBarsShown (getComponent()->isVerticalScrollBarShown(), oldState);
354 changed();
355 return true;
358 bool vertical, newState, oldState;
362 //==============================================================================
363 class ViewportScrollbarSizeProperty : public SliderPropertyComponent,
364 public ChangeListener
366 public:
367 ViewportScrollbarSizeProperty (Viewport* comp, JucerDocument& document_)
368 : SliderPropertyComponent ("scrollbar size", 3.0, 30.0, 1.0, 1.0),
369 component (comp),
370 document (document_)
372 document.addChangeListener (this);
375 ~ViewportScrollbarSizeProperty()
377 document.removeChangeListener (this);
380 void setValue (double newValue)
382 document.getUndoManager().undoCurrentTransactionOnly();
384 document.perform (new ViewportScrollbarSizeChangeAction (component, *document.getComponentLayout(), roundToInt (newValue)),
385 "Change Viewport scrollbar size");
388 double getValue() const
390 return component->getScrollBarThickness();
393 void changeListenerCallback (ChangeBroadcaster*)
395 refresh();
398 Viewport* component;
399 JucerDocument& document;
401 private:
402 class ViewportScrollbarSizeChangeAction : public ComponentUndoableAction <Viewport>
404 public:
405 ViewportScrollbarSizeChangeAction (Viewport* const comp, ComponentLayout& layout, const int newState_)
406 : ComponentUndoableAction <Viewport> (comp, layout),
407 newState (newState_)
409 oldState = comp->getScrollBarThickness();
412 bool perform()
414 showCorrectTab();
415 getComponent()->setScrollBarThickness (newState);
416 changed();
417 return true;
420 bool undo()
422 showCorrectTab();
423 getComponent()->setScrollBarThickness (newState);
424 changed();
425 return true;
428 int newState, oldState;
432 //==============================================================================
433 class ViewportContentTypeProperty : public ComponentChoiceProperty <Viewport>
435 public:
436 ViewportContentTypeProperty (Viewport* comp, JucerDocument& document)
437 : ComponentChoiceProperty <Viewport> ("content", comp, document)
439 choices.add ("No content component");
440 choices.add ("Jucer content component");
441 choices.add ("Named content component");
444 void setIndex (int newIndex)
446 document.perform (new ViewportContentTypeChangeAction (component, *document.getComponentLayout(), newIndex),
447 "Change Viewport content type");
450 int getIndex() const
452 return getViewportContentType (component);
455 private:
456 class ViewportContentTypeChangeAction : public ComponentUndoableAction <Viewport>
458 public:
459 ViewportContentTypeChangeAction (Viewport* const comp, ComponentLayout& layout, const int newValue_)
460 : ComponentUndoableAction <Viewport> (comp, layout),
461 newValue (newValue_)
463 oldValue = getViewportContentType (comp);
466 bool perform()
468 showCorrectTab();
469 setViewportContentType (getComponent(), newValue);
470 changed();
471 layout.getDocument()->refreshAllPropertyComps();
472 return true;
475 bool undo()
477 showCorrectTab();
478 setViewportContentType (getComponent(), oldValue);
479 changed();
480 layout.getDocument()->refreshAllPropertyComps();
481 return true;
484 int newValue, oldValue;
488 //==============================================================================
489 class ViewportJucerFileProperty : public FilePropertyComponent,
490 public ChangeListener
492 public:
493 ViewportJucerFileProperty (Viewport* const component_, JucerDocument& document_)
494 : FilePropertyComponent ("Jucer file", false, true),
495 component (component_),
496 document (document_)
498 document.addChangeListener (this);
501 ~ViewportJucerFileProperty()
503 document.removeChangeListener (this);
506 //==============================================================================
507 void setFile (const File& newFile)
509 document.perform (new JucerCompFileChangeAction (component, *document.getComponentLayout(),
510 newFile.getRelativePathFrom (document.getFile().getParentDirectory())
511 .replaceCharacter ('\\', '/')
513 "Change Jucer component file");
516 const File getFile() const
518 const String filename (getViewportJucerComponentFile (component));
520 if (filename.isEmpty())
521 return File::nonexistent;
523 return document.getFile().getSiblingFile (filename);
526 void changeListenerCallback (ChangeBroadcaster*)
528 refresh();
531 private:
532 Viewport* const component;
533 JucerDocument& document;
535 class JucerCompFileChangeAction : public ComponentUndoableAction <Viewport>
537 public:
538 JucerCompFileChangeAction (Viewport* const comp, ComponentLayout& layout, const String& newState_)
539 : ComponentUndoableAction <Viewport> (comp, layout),
540 newState (newState_)
542 oldState = getViewportJucerComponentFile (comp);
545 bool perform()
547 showCorrectTab();
548 setViewportJucerComponentFile (getComponent(), newState);
549 changed();
550 return true;
553 bool undo()
555 showCorrectTab();
556 setViewportJucerComponentFile (getComponent(), oldState);
557 changed();
558 return true;
561 String newState, oldState;
565 //==============================================================================
566 class ViewportContentClassProperty : public ComponentTextProperty <Viewport>
568 public:
569 ViewportContentClassProperty (Viewport* comp, JucerDocument& document)
570 : ComponentTextProperty <Viewport> ("content class", 256, false, comp, document)
574 void setText (const String& newText)
576 document.perform (new ViewportClassNameChangeAction (component, *document.getComponentLayout(), newText),
577 "Change Viewport content class");
580 const String getText() const
582 return getViewportGenericComponentClass (component);
585 private:
586 class ViewportClassNameChangeAction : public ComponentUndoableAction <Viewport>
588 public:
589 ViewportClassNameChangeAction (Viewport* const comp, ComponentLayout& layout, const String& newValue_)
590 : ComponentUndoableAction <Viewport> (comp, layout),
591 newValue (newValue_)
593 oldValue = getViewportGenericComponentClass (comp);
596 bool perform()
598 showCorrectTab();
599 setViewportGenericComponentClass (getComponent(), newValue);
600 changed();
601 layout.getDocument()->refreshAllPropertyComps();
602 return true;
605 bool undo()
607 showCorrectTab();
608 setViewportGenericComponentClass (getComponent(), oldValue);
609 changed();
610 layout.getDocument()->refreshAllPropertyComps();
611 return true;
614 String newValue, oldValue;
618 //==============================================================================
619 class ConstructorParamsProperty : public ComponentTextProperty <Viewport>
621 public:
622 ConstructorParamsProperty (Viewport* comp, JucerDocument& document)
623 : ComponentTextProperty <Viewport> ("constructor params", 512, false, comp, document)
627 void setText (const String& newText)
629 document.perform (new ConstructorParamChangeAction (component, *document.getComponentLayout(), newText),
630 "Change Viewport content constructor params");
633 const String getText() const
635 return getViewportConstructorParams (component);
638 private:
639 int tabIndex;
641 class ConstructorParamChangeAction : public ComponentUndoableAction <Viewport>
643 public:
644 ConstructorParamChangeAction (Viewport* const comp, ComponentLayout& layout, const String& newValue_)
645 : ComponentUndoableAction <Viewport> (comp, layout),
646 newValue (newValue_)
648 oldValue = getViewportConstructorParams (comp);
651 bool perform()
653 showCorrectTab();
654 setViewportConstructorParams (getComponent(), newValue);
655 changed();
656 layout.getDocument()->refreshAllPropertyComps();
657 return true;
660 bool undo()
662 showCorrectTab();
663 setViewportConstructorParams (getComponent(), oldValue);
664 changed();
665 layout.getDocument()->refreshAllPropertyComps();
666 return true;
669 String newValue, oldValue;
675 #endif // __JUCER_VIEWPORTHANDLER_JUCEHEADER__